package org.dyk.mallmanagementsystem.controller;

import org.dyk.mallmanagementsystem.repository.GoodsRepository;
import org.dyk.mallmanagementsystem.model.Goodstype;
import org.dyk.mallmanagementsystem.service.goodstype.GoodstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import java.util.List;

@Controller
@RequestMapping(value = {"/goodstype"})
public class GoodstypeController {
    @Autowired
    private GoodstypeService goodstypeService;
    @Autowired
    private GoodsRepository goodsDao;

    /**
     * 商品类型管理 页面
     * @param model
     * @return
     */
    @RequestMapping(value = {""})
    public String goodsType(@RequestParam(required = true, defaultValue = "0") int page,
                            @RequestParam(required = true, defaultValue = "10") int size,
                            Model model){
        /**
         * 分页方法一，用MyBatis插件——PageHelper插件
         */
//        //分页
//        PageHelper.startPage(page, 5);
//        //查询所有类型信息
//        List<Goodstype> all = service.selectAll();
//        //存入PageInfo
//        PageInfo<Goodstype> pageInfo = new PageInfo<Goodstype>(all);
//        model.addAttribute("goodstypeList",all);
//        model.addAttribute("pageInfo",pageInfo);
//        return "goodstype";

        /**
         * 分页方法二，用spring Data Jpa 自带的分页方法
         */
        //分页大小
        Pageable pageable = PageRequest.of(page, size);
        //所有商品分页查询
        Page<Goodstype> pageAll = goodstypeService.findAll(pageable);
        //将分页包传到前端
        model.addAttribute("pageAll",pageAll);
        return "goodstype";


    }

    /**
     * 添加商品分类 页面
     * @return
     */
    @RequestMapping(value = {"/insertGoodstype"})
    public String insertGoodstype(){
        return "insertGoodstype";
    }

    /**
     * 添加商品分类 验证
     * @param goodstypeName
     * @param goodstypeDescribe
     * @return
     * 返回boolean一定要加上@ResponseBody
     */
    @RequestMapping(value = {"/insert"}, method = RequestMethod.POST)
    @ResponseBody
    public boolean insert(@RequestParam String goodstypeName,
                          @RequestParam String goodstypeDescribe){
        boolean inser = goodstypeService.inser(goodstypeName, goodstypeDescribe);
        if (inser){
            System.out.println("商品分类添加成功！");
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改商品 页面
     * @param tid
     * @param model
     * @return
     */
    @RequestMapping(value = {"/updataGoodstype"})
    public String updataGoodstype(@RequestParam int tid, Model model){
        //根据主键获取商品分类信息
        Goodstype byTid = goodstypeService.findByTid(tid);
        model.addAttribute("goodstype",byTid);
        return "updataGoodstype";
    }

    /**
     * 修改商品 验证
     * @param tid
     * @param goodstypeName
     * @param goodstypeDescribe
     * @return
     */
    @RequestMapping(value = {"/updata"})
    @ResponseBody
    public boolean updata(@RequestParam int tid,
                          @RequestParam String goodstypeName,
                          @RequestParam String goodstypeDescribe){
        boolean updata = goodstypeService.updata(tid, goodstypeName, goodstypeDescribe);
        if (updata){
            System.out.println("商品分类修改成功！");
            return true;
        }else {
            return false;
        }
    }

    /**
     * 商品分类删除 验证
     * @param tid
     * @param modelMap
     * @return
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam int tid, RedirectAttributesModelMap modelMap){
        boolean delete = goodstypeService.delete(tid);
        if (delete){
            modelMap.addFlashAttribute("delMsg","删除成功");
            return "redirect:/goodstype";
        }else {
            modelMap.addFlashAttribute("delMsg","删除失败，该分类下还有商品");
            return "redirect:/goodstype";
        }
    }

    /**
     * 异步获取所有商品分类信息
     * @return
     */
    @RequestMapping(value = {"/getAllGoodstype"}, method = RequestMethod.POST)
    @ResponseBody
    public List<Goodstype> getAllGoodstype(){
        return goodstypeService.findAll();
    }

    /**
     * 自定义查询商品分类
     * @param selectByGoodstypeName
     * @return
     */
    @RequestMapping(value = {"/selectGoodstype"})
    @ResponseBody
    public List<Goodstype> selectGoodstype(@RequestParam String selectByGoodstypeName){
        return goodstypeService.selectByGoodstypeName(selectByGoodstypeName);
    }
}
